package cz.muni.fi.pa165.mushroom.website.servicesImpl;


import cz.muni.fi.pa165.mushroom.website.dao.MushroomDao;
import cz.muni.fi.pa165.mushroom.website.dto.MushroomDto;
import cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO;
import cz.muni.fi.pa165.mushroom.website.entities.Mushroom;
import cz.muni.fi.pa165.mushroom.website.entities.MushroomType;
import cz.muni.fi.pa165.mushroom.website.services.MushroomService;
import cz.muni.fi.pa165.mushroom.website.transformerAdapter.Transformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;

/**
 * @author: Pavol Donko
 */

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MushroomServiceImpl implements MushroomService {

    @Autowired
    private MushroomDao mushroomDao;

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void createMushroom(MushroomDto mushroomDto) {
        Mushroom mushroom = Transformer.transformMushroomDtoOnMushroom(mushroomDto);
        mushroomDao.createMushroom(mushroom);
        mushroomDto.setId(mushroom.getMushroomId());
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void updateMushroom(MushroomDto mushroomDto) {
        mushroomDao.updateMushroom(Transformer.transformMushroomDtoOnMushroom(mushroomDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void deleteMushroom(MushroomDto mushroomDto) {
        mushroomDao.removeMushroom(Transformer.transformMushroomDtoOnMushroom(mushroomDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public MushroomDto findMushroom(Long id) {
        return Transformer.transformMushroomOnMushroomDto(mushroomDao.findMushroom(id));
    }

    @Override
    @Transactional
    public List<MushroomDto> listMushrooms() {
        List<MushroomDto> dtos = new ArrayList<>();
        List<Mushroom> test = mushroomDao.listMushrooms();
        for(Mushroom mushroom : test){
            dtos.add(Transformer.transformMushroomOnMushroomDto(mushroom));
        }
        return dtos;
    }  

    @Override
    public List<MushroomDto> listMushroomsWithType(MushroomTypeDTO type) {
        List<MushroomDto> dtos = new ArrayList<>();
        List<Mushroom> temp = null;
        switch (type){
           case EDIBLE: temp = mushroomDao.listMushroomsWithType(MushroomType.EDIBLE); break;
           case NOTEDIBLE: temp = mushroomDao.listMushroomsWithType(MushroomType.NOTEDIBLE); break;
           case POISONOUS: temp = mushroomDao.listMushroomsWithType(MushroomType.POISONOUS);
        }

        for(Mushroom mushroom : temp){
            dtos.add(Transformer.transformMushroomOnMushroomDto(mushroom));
        }
        return dtos;
    }

    @Override
    public List<MushroomDto> listMushroomsWithName(String name) {
        List<MushroomDto> dtos = new ArrayList<>();
        for(Mushroom mushroom : mushroomDao.listMushroomsWithName(name)){
           dtos.add(Transformer.transformMushroomOnMushroomDto(mushroom)); 
        }
       return dtos;
    }
    
    

    public void setMushroomDao(MushroomDao mushroomDao) {
        this.mushroomDao = mushroomDao;
    }

    public MushroomDao getMushroomDao() {
        return mushroomDao;
    }
}
